package com.situ.web.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.Dept;
import com.situ.web.util.JDBCUtil;

import jdk.nashorn.internal.runtime.regexp.joni.SearchAlgorithm;

/**
 * 做和Dept相关的增删改查的操作
 *
 */
// http://localhost:8080/JavaWeb1907/dept_list.jsp
// http://localhost:8080/JavaWeb1907/dept
@WebServlet("/dept")
public class DeptServlet extends HttpServlet{

	// 访问Servlet时候默认访问service这个方法
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("DeptServlet.service()");
		// 解决post请求乱码问题
		req.setCharacterEncoding("UTF-8");
		// /JavaWeb1907/dept?method=deleteById&id=1
		// /JavaWeb1907/dept?method=update
		// /JavaWeb1907/dept?method=findAll
		String method = req.getParameter("method");
		if (method == null || method.equals("")) {
			method = "findAll";
		}
		
		switch (method) {
		case "findAll":
			findAll(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "add":
			add(req, resp);
			break;
		case "toUpdate":
			toUpdate(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "search":
			search(req, resp);
			break;
		default:
			break;
		}
		
	}
	

	private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("DeptServlet.search()");
		String searchName = req.getParameter("searchName");
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Dept> list = new ArrayList<Dept>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,name FROM dept where name like ?";
			preparedStatement = connection.prepareStatement(sql);
			// 豆豆加加
			preparedStatement.setString(1, "%" + searchName + "%");
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Dept dept = new Dept(id, name);
				list.add(dept);
			}
			
			for (Dept dept : list) {
				System.out.println(dept);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		// 把list集合放到内存区域里面
		req.setAttribute("list", list);
		// 转发到dept_list.jsp进行展示
		req.getRequestDispatcher("dept_list.jsp").forward(req, resp);
	}


	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "UPDATE dept SET name=? WHERE id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, name);
			preparedStatement.setInt(2, Integer.parseInt(id));
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("更新成功");
			} else {
				System.out.println("更新失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		
		// 重定向到列表界面
		// /JavaWeb1907/dept?method=findAll
		resp.sendRedirect("/JavaWeb1907/dept?method=findAll");
	}


	private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = req.getParameter("id");
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Dept dept = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,name FROM dept where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, Integer.parseInt(id));
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				String name = resultSet.getString("name");
				dept = new Dept(Integer.parseInt(id), name);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		// 把dept放到内存区域里面
		req.setAttribute("dept", dept);
		// 转发到dept_update.jsp进行展示
		req.getRequestDispatcher("dept_update.jsp").forward(req, resp);
	}


	private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		System.out.println("DeptServlet.add()");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO dept(name) VALUES(?)";
			preparedStatement = connection.prepareStatement(sql);
			// 把？位置填上值
			preparedStatement.setString(1, name);
			System.out.println(preparedStatement);
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		
		// 重定向到列表界面
		// /JavaWeb1907/dept?method=findAll
		resp.sendRedirect("/JavaWeb1907/dept?method=findAll");
		
		// req.getRequestDispatcher("/dept?method=findAll").forward(req, resp);
	}



	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("DeptServlet.deleteById()");
		String id = req.getParameter("id");
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "DELETE FROM dept WHERE id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, Integer.parseInt(id));
			System.out.println(preparedStatement);
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		
		// 重定向到列表界面
		// /JavaWeb1907/dept?method=findAll
		resp.sendRedirect("/JavaWeb1907/dept?method=findAll");
	}



	private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("DeptServlet.findAll()");
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Dept> list = new ArrayList<Dept>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,name FROM dept";
			preparedStatement = connection.prepareStatement(sql);
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Dept dept = new Dept(id, name);
				list.add(dept);
			}
			
			for (Dept dept : list) {
				System.out.println(dept);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		// 把list集合放到内存区域里面
		req.setAttribute("list", list);
		// 转发到dept_list.jsp进行展示
		req.getRequestDispatcher("dept_list.jsp").forward(req, resp);
	}
}
